﻿import { Component, Injector, ViewChild} from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { RequirementManagerItemEditComponent } from './requirement-manager-item_edit.component'
import { RequirementForModelDto,RequirementProductionServiceProxy,RequirementPackageState,UserServiceProxy } from '@shared/service-proxies/service-proxies';
import { AbpSessionService } from '@abp/session/abp-session.service';

@Component({
    templateUrl: './requirement-manager.component.html',
    styleUrls: ['./requirement-manager.component.less'],
    animations: [appModuleAnimation()]
})
export class RequirementManagerComponent extends AppComponentBase{
    @ViewChild('requirementManagerItemEditComponent') requirementManagerItemEditComponent: RequirementManagerItemEditComponent;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    tabObject:{
        filterText:""
    };
    loading = false;
    caches: any = null;
    logs: any = '';
    filterText = '';
    hidden = true;
    selectAll:boolean=true;
    userName:string;

    constructor(
        injector: Injector,
        private _requirementProductionServiceProxy: RequirementProductionServiceProxy,
        private _userServiceProxy: UserServiceProxy,
        private _abpSessionService :AbpSessionService) {
        super(injector);
    }

    clearTab(): void {

    }
    getUserName(_pRequirementForModelDto : RequirementForModelDto): void {
        let id:number = this._abpSessionService.userId;
        this._userServiceProxy.getUserForEdit(id) .pipe(finalize(() => { }))
        .subscribe(result=> {
            this.userName = result.user.userName;
            if(this.userName){
                this.createOrEditRequirementItem(_pRequirementForModelDto);
            }
        });
    }
    createOrEditRequirementItem(_pRequirementForModelDto : RequirementForModelDto):void{
        if(!this.userName){
            this.getUserName(_pRequirementForModelDto);
            return;
        }
        this.requirementManagerItemEditComponent.show(_pRequirementForModelDto,this.userName);
    }
    /**
     * 检索所有需求
     * @param event 
     */
    getRequirementForTaskList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this.primengTableHelper.showLoadingIndicator();

        this._requirementProductionServiceProxy.getRequirementForTaskList(
            this.filterText,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event))
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    
    getRequirementPackageStateString(state:number):string{
        return RequirementPackageState[state];
    }
    onResize(event): void {
        
    }

    ngOnInit(): void {
        const self = this;
    }
}
